<template>
	<view class="home">
		<view class="home-but">
			<u-button shape="circle" size="medium" plain type="primary" @click="establish">创建工程</u-button>
			<u-button shape="circle" size="medium" plain type="warning" @click="joinProject">加入工程</u-button>
		</view>
		<view class="content" v-if="showImg">
			<u-card margin="20rpx" v-for="(item, index) of homeInfo" :key="index" @click="projectClick(item.id)" :border="false" :title="item.projectName" :thumb="thumb">
				<view slot="body" >
					<view class="textColor u-body-item u-flex u-col-between u-padding-bottom-20">
						<view class="u-body-item-title">工程地址: <text>{{ item.projectAddr }}</text></view>
					</view>
					<view class="textColor u-body-item u-flex u-col-between u-padding-bottom-20">
						<view class="u-body-item-title">工程编号: <text>{{ item.projectNo }}</text></view>
					</view>
					<view class="textColor u-body-item u-flex u-col-between u-p-t-0">
						<view class="u-body-item-title">创建时间: <text>{{ item.createTime }}</text></view>
					</view>
				</view>
			</u-card>
			
		<u-loadmore v-if="loadShow" bg-color="#F5F5F5" :status="status" :icon-type="iconType" :load-text="loadText" />
		</view>
		<view class="noImg" v-else>
			<view class="line"></view>
			<view class="backgroundImg u-text-center">
				<image src="../../static/images/home_background.png" mode=""></image>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				thumb: require('../../static/images/home_icon.png'),
				homeInfo: {},
				size: 5,
				current: 1,
				status: 'loading',
				iconType: 'flower',
				loadShow: false,
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				showImg: false // 首页是否显示有无数据照片
			}
		},
		onLoad() {
			// 判断是否有权限，为false跳转登录页面
			let isUserTrue = uni.getStorageSync("isUserTrue") || '';	
			if(isUserTrue == 'true') {
			} else {
				this.$navJump.reLaunch('/pageLogin/login');	
			}
		},
		onShow() {
			this.getHomeData() // 获取首页数据
		},
		created() {
			this.getHomeData() // 获取首页数据
		},
		methods: {
			// 获取首页内容
			getHomeData(current) {
				let header = {
					"content-type":"application/x-www-form-urlencoded;charset=UTF-8"
				}
				let dataList = {
					size: this.size,
					current: current || this.current
				}
				this.$ajax.post({
					url: '/project-info/page',
					data: dataList,
					header: header
				}).then(res => {
					// console.log(res)
					if(res.code == 200) {
						if(res.data.records.length > 0 && this.current == 1) {
							// console.log('第一次加载')
							this.homeInfo = res.data.records
						}else if(res.data.records.length > 0 &&current > 1) {
							// console.log('上拉加载')
							setTimeout(() => {
								this.loadShow = true
								this.status = 'loading'
								this.homeInfo = this.homeInfo.concat(res.data.records)
							}, 500)
						} else if(current > 1 && res.data.records.length == 0) {
							// console.log('没有更多了')
							this.loadShow = true
							this.status = 'nomore'
						}  
					
						this.homeInfo = this.homeInfo.map(item => {
							if(item.projectStatus == 2) {
								return item
							} else {
								return {}
							}
						})
						this.homeInfo = this.homeInfo.filter(n => {
							if(n.createTime) {
								return n
							}
						})
						if(this.homeInfo.length > 0){
							this.showImg = true
						} else {
							this.showImg = false
						}
					}
				})
			},
			// 下拉刷新
			onPullDownRefresh() {
				// console.log('下拉刷新')
				this.getHomeData() // 获取首页数据
				setTimeout(() => {
					uni.stopPullDownRefresh() // 关闭下拉刷新
					this.$refs.uToast.show({
						title: '列表刷新成功！',
						type: 'success'
					})
				}, 500)
			},
			// 上拉加载
			onReachBottom() {
				let that = this
				that.current ++
				this.getHomeData(that.current)
			},
			// 创建工程
			establish() {
				this.$navJump.navigateTo('../../pageHome/createProject')
				// this.$u.toast('adasda');
			},
			// 加入工程
			joinProject() {
				this.$navJump.navigateTo('../../pageHome/search')
			},
			// 查看项目
			projectClick(id) {
				this.$navJump.navigateTo('/pageHome/projectDetails?idx=' + id)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home {
		.home-but{
			position: fixed;
			top: 0;
			width: 100%;
			z-index: 99;
			background: #FFFFFF;
			padding: 22rpx;
			u-button:first-child {
				margin-right: 20rpx;
			}
			/deep/.u-size-medium {
				padding: 0 30px!important;
			}
		}
		.noImg {
			background: #FFFFFF;
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			.line {
				padding-top: 140rpx;
				background: #F5F5F5;
				height: 20rpx;
				width: 100%;
			}
			.backgroundImg{
				height: auto;
				width: 90%;
				height: 500rpx;
				margin: 64rpx 5% 80rpx;
			}
		}

		.content {
			margin-top: 134rpx;
			.textColor {
				color: #8C8C8C;
				text {
					margin-left: 20rpx;
					display: inline-block;
				}
			}
		}
	}
	/deep/.u-load-more-wrap {
		padding: 0 0 20rpx;
	}
</style>
 



 
 
 